package org.shangma.service.impl;

import io.seata.core.context.RootContext;
import org.shangma.common.Good;
import org.shangma.mapper.GoodMapper;
import org.shangma.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodMapper goodMapper;

    @Override
    public Good findGoodById(int goodId) {
        Good good = goodMapper.findGoodById(goodId);
        return good;
    }

    @Override
    public void reduceStock(int goodId, int stockNum) {
        System.out.println("是否在全局事务中：" + RootContext.inGlobalTransaction());
        System.out.println("事务id: " + RootContext.getXID());
        System.out.println("事务类型: " + RootContext.getBranchType());
        Good good = goodMapper.findGoodById(goodId);
        if(good.getGoodStock()<stockNum){
            throw new RuntimeException("库存不足");
        }
        goodMapper.reduceStock(goodId,stockNum);
    }
}
